package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Category;
import com.itheima.domain.CategoryExample;
import com.itheima.domain.Movies;
import com.itheima.domain.MoviesExample;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.MoviesMapper;
import com.itheima.service.MoviesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MoviesServiceImpl implements MoviesService {
    @Autowired
    private MoviesMapper moviesMapper;

    @Override
    public PageInfo<Movies> findByPage(Integer pageNum, Integer pageSize) {
        //设置分页查询条件
        PageHelper.startPage(pageNum,pageSize);
        //设置排序条件
        MoviesExample moviesExample = new MoviesExample();
        moviesExample.setOrderByClause("show_time desc");

        //执行查询所有
        List<Movies> list = moviesMapper.selectByExample(moviesExample);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Movies> findByPage(Integer pageNum, Integer pageSize, Integer cid, String keyword) {
        //设置分页查询条件
        PageHelper.startPage(pageNum,pageSize);
        //设置排序条件
        MoviesExample moviesExample = new MoviesExample();
        moviesExample.setOrderByClause("show_time desc");
        MoviesExample.Criteria criteria = moviesExample.createCriteria();//封装查询条件对象
        //设置查询条件
        if(cid!=null){
            criteria.andCidEqualTo(cid);
        }
        if(keyword!=null && !"".equals(keyword)){
            criteria.andKeywordLike("%"+keyword+"%");
        }

        //执行查询所有
        List<Movies> list = moviesMapper.selectByExample(moviesExample);
        return new PageInfo<>(list);
    }

    @Override
    public void save(Movies movies) {
        //获取影视id
        Integer id = movies.getId();
        if(id==null){
            //新增影视数据
            moviesMapper.insertSelective(movies);
        }else{
            //修改影视数据
            moviesMapper.updateByPrimaryKeySelective(movies);
        }

    }

    @Override
    public Movies findById(Integer id) {
        return moviesMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(Integer id) {
        moviesMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        for (Integer id : ids) {
            moviesMapper.deleteByPrimaryKey(id);
        }
    }

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Map<String, List<Movies>> findMoviesMap() {
        //创建封装数据的map集合
        Map<String, List<Movies>> map = new LinkedHashMap<>();
        //1、查询所有的分类数据
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("order_no asc");
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        //2、查询当前分类关联的影视列表数据
        for (Category category : categoryList) {
            //默认查询前6条影视列表数据
            PageHelper.startPage(1,6);
            //获取分类名称
            String categoryName = category.getCategoryName();
            //获取分类的id
            Integer cid = category.getId();
            //查询当前分类关联的映射列表数据
            MoviesExample moviesExample = new MoviesExample();
            moviesExample.createCriteria().andCidEqualTo(cid);
            //基于上映时间倒序排序
            moviesExample.setOrderByClause("show_time DESC");
            List<Movies> moviesList = moviesMapper.selectByExample(moviesExample);
            map.put(categoryName,moviesList);
        }

        return map;
    }
}
